//
//  KnowledgeViewController.m
//  ShiWuPai
//
//  Created by app on 16/11/24.
//  Copyright © 2016年 QQ:244987427. All rights reserved.
//

#import "KnowledgeViewController.h"

#import <Masonry.h>
#import <SVPullToRefresh.h>

#import "BaseNetworkRequest.h"
#import "BaseNetworkRequest+GuangChi.h"

#import "WHEvaluatingTableViewCell.h"

#import "WHFeedInfor.h"
@interface KnowledgeViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) int page;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation KnowledgeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor blueColor];
    [self addSubViews];
    //添加下拉刷新，上拉加载
    __weak typeof (KnowledgeViewController) *weakSelf=self;
    [self.tableView addPullToRefreshWithActionHandler:^{
        weakSelf.page=1;
        [weakSelf requestData];
        
    }];
    
    //设置下拉刷新view。。。。。。
    [self.tableView addInfiniteScrollingWithActionHandler:^{
        weakSelf.page++;
        [weakSelf requestData];
    }];
    
    //设置上拉加载标题。。。。。。
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.page=1;
    [self requestData];
}



#pragma network request
-(void)requestData{
    
    [BaseNetworkRequest getFeedInfoWithPage:self.page CateGory:3 success:^(id responseObj, NSError *error) {
        WHFeedInfor *feedInfor=responseObj;
        if (!error) {
            if (self.page==1) {
                //此时是下拉刷新
                [self.dataArray removeAllObjects];
                [self.dataArray addObjectsFromArray:feedInfor.feeds];
                [self.tableView.pullToRefreshView stopAnimating];
                [self.tableView reloadData];
            }
            
            else{
                //此时是上拉加载,向原数据中追加数据
                [self.dataArray addObjectsFromArray:feedInfor.feeds];
                [self.tableView.infiniteScrollingView stopAnimating];
                [self.tableView reloadData];
            }
        }
        
        else{
            //show Error
            NSLog(@"error:%@",error);
        }
        
    }];
}
#pragma mark - initViews
-(void)addSubViews{
    self.tableView=[[UITableView alloc] init];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.view);
        make.leading.trailing.equalTo(self.view);
    }];
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.tableView registerNib:[UINib nibWithNibName:@"WHKnowLedgeTabViewCell" bundle:nil] forCellReuseIdentifier:@"WHKnowLedgeTabViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WHKnowledgeTabViewPictureCell" bundle:nil] forCellReuseIdentifier:@"WHKnowledgeTabViewPictureCell"];
}

-(void)addConstraints{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.view);
        make.leading.equalTo(self.view).offset(8);
        make.trailing.equalTo(self.view).offset(-8);
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WHFeed *feed=self.dataArray[indexPath.row];
    WHEvaluatingTableViewCell *cell=nil;
    if (feed.images.count==1) {
        cell=[tableView dequeueReusableCellWithIdentifier:@"WHKnowLedgeTabViewCell" forIndexPath:indexPath];
    }
    
    else{
        cell=[tableView dequeueReusableCellWithIdentifier:@"WHKnowledgeTabViewPictureCell" forIndexPath:indexPath];
    }
    cell.feed=feed;
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 145;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //跳转
    WHFeed *feed=self.dataArray[indexPath.row];
    DetailsViewController *detailsViewController=[[DetailsViewController alloc] init];
    detailsViewController.linkURL=[NSURL URLWithString:feed.link];
    [self.navigationController pushViewController:detailsViewController animated:YES];
}

#pragma mark - setter getter
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}

@end
